package com.sy.musicapp.ui.home

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Search
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.sy.musicapp.model.HotKey
import com.sy.musicapp.ui.common.noRippleClickable
import com.sy.musicapp.ui.theme.Global
import com.sy.musicapp.ui.theme.MusicAppTheme
import com.sy.musicapp.ui.theme.tintText

@ExperimentalComposeUiApi
@ExperimentalAnimationApi
@Composable
fun SearchBar(hotkey: HotKey, onSearch: (String) -> Unit) {
    var str by remember { mutableStateOf("") }
    var isFocus by remember { mutableStateOf(false) }
    val localFocusManager = LocalFocusManager.current
    Row(verticalAlignment = Alignment.CenterVertically) {
        BasicTextField(
            modifier = Modifier
                .weight(0.9f)
                .clip(Global.shapes.large)
                .background(Color.Black.copy(alpha = 0.1f))
                .onFocusChanged {
                    isFocus = it.isFocused
                    str = ""
                }
                .padding(5.dp),
            value = str,
            onValueChange = { str = it },
            singleLine = true,
            textStyle = tintText,
            cursorBrush = SolidColor(Global.colors.onPrimary),
            keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
            keyboardActions = KeyboardActions(onSearch = {
                if (str.isBlank()) {
                    onSearch(hotkey.realkeyword)
                    return@KeyboardActions
                }
                onSearch(str)
            }),
            decorationBox = { innerTextField ->
                Row(
                    horizontalArrangement = Arrangement.Start,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Icon(
                        modifier = Modifier.scale(0.6f),
                        imageVector = Icons.Rounded.Search,
                        contentDescription = "search",
                        tint = Color.Gray
                    )
                    if (str.isEmpty() && !isFocus) {
                        Text(
                            text = hotkey.showKeyword,
                            style = tintText,
                        )
                    }
                    innerTextField()
                }
            }
        )
        AnimatedVisibility(visible = isFocus) {
            Text(text = "取消",
                color = Global.colors.onSurface,
                modifier = Modifier
                    .padding(start = 10.dp)
                    .weight(0.1f)
                    .noRippleClickable {
                        str = ""
                        isFocus = false
                        localFocusManager.clearFocus()
                    })
        }
    }

}

@ExperimentalComposeUiApi
@ExperimentalAnimationApi
@Preview()
@Composable
fun SearchPrev() {
    MusicAppTheme {
        SearchBar(HotKey.Empty) {}
    }
}

@ExperimentalComposeUiApi
@ExperimentalAnimationApi
@Preview()
@Composable
fun SearchPrevDark() {
    MusicAppTheme(darkTheme = true) {
        SearchBar(HotKey.Empty) {}
    }
}